package org.abettor.tanqiu.gameloop.object;

import org.abettor.tanqiu.gameloop.Constants;

import com.badlogic.gdx.math.Vector2;

public class BallMapSpherePart extends BallMapPart {
	public static final int TYPE_1 = 0, TYPE_2 = 1, TYPE_3 = 3, TYPE_4 = 4,
			TYPE_5 = 5, TYPE_6 = 6, TYPE_7 = 7, TYPE_8 = 8;// 球形地图的类型
	public static final int POT_TYPE_START = 0, POT_TYPE_END = 1;
	private int type;// 地图的类型
	private Vector2 circleDot;// 圆点坐标
	private Vector2 begin, end;// 起始坐标,终点坐标

	/**
	 * 构造函数
	 * 
	 * @param type
	 * @param circlePot
	 * @param potType
	 *            跟坐标平行点为开始和结束
	 */
	public BallMapSpherePart(int type, Vector2 circlePot, int potType) {
		float tempRadius = Constants.SPHEREPART_RAIDUS - Constants.BALL_RAIDUS;
		this.type = type;
		this.circleDot = circlePot;
		Vector2 point1, point2;
		this.type = type;
		this.circleDot = circlePot;
		switch (type) {
		case TYPE_1:
			point1 = new Vector2();
			point1.x = circlePot.x - tempRadius;
			point1.y = circlePot.y;
			point2 = new Vector2();
			point2.x = circlePot.x
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2.y = circlePot.y
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			if (potType == POT_TYPE_START) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_2:
			point1 = new Vector2();
			point1.x = circlePot.x
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point1.y = circlePot.y
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2 = new Vector2();
			point2.x = circlePot.x;
			point2.y = circlePot.y + tempRadius;
			if (potType == POT_TYPE_END) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_3:
			point1 = new Vector2();
			point1.x = circlePot.x;
			point1.y = circlePot.y + tempRadius;
			point2 = new Vector2();
			point2.x = circlePot.x
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2.y = circlePot.y
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			if (potType == POT_TYPE_START) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_4:
			point1 = new Vector2();
			point1.x = circlePot.x
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point1.y = circlePot.y
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2 = new Vector2();
			point2.x = circlePot.x + tempRadius;
			point2.y = circlePot.y;
			if (potType == POT_TYPE_END) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_5:
			point1 = new Vector2();
			point1.x = circlePot.x + tempRadius;
			point1.y = circlePot.y;
			point2 = new Vector2();
			point2.x = circlePot.x
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2.y = circlePot.y
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			if (potType == POT_TYPE_START) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_6:
			point1 = new Vector2();
			point1.x = circlePot.x
					+ (float) (Math.sin(Math.PI / 4) * tempRadius);
			point1.y = circlePot.y
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2 = new Vector2();
			point2.x = circlePot.x;
			point2.y = circlePot.y - tempRadius;
			if (potType == POT_TYPE_END) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_7:
			point1 = new Vector2();
			point1.x = circlePot.x;
			point1.y = circlePot.y - tempRadius;
			point2 = new Vector2();
			point2.x = circlePot.x
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2.y = circlePot.y
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			if (potType == POT_TYPE_START) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		case TYPE_8:
			point1 = new Vector2();
			point1.x = circlePot.x
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point1.y = circlePot.y
					- (float) (Math.sin(Math.PI / 4) * tempRadius);
			point2 = new Vector2();
			point2.x = circlePot.x - tempRadius;
			point2.y = circlePot.y;
			if (potType == POT_TYPE_END) {
				begin = point1;
				end = point2;
			} else {
				begin = point2;
				end = point1;
			}
			break;
		}
	}
	@Override
	public Vector2 nextStep(Vector2 start, int step) {
		Vector2 retVector = new Vector2();
		float beginX = start.x;
		if (end.x > begin.x) {
			beginX = beginX + step;
			if(beginX > end.x){
				return null;
			}
		} else {
			beginX = beginX - step;
			if(beginX < start.x){
				return null;
			}
		}
		retVector.x = beginX;
		retVector.y = squareRoot(beginX);
		return retVector;
	}

	private float squareRoot(float x) {
		float tmp = Constants.SPHEREPART_RAIDUS*Constants.SPHEREPART_RAIDUS - (x - circleDot.x) * (x - circleDot.x);
		float retVal = 0;
		switch (type) {
		case TYPE_1:
		case TYPE_2:
		case TYPE_3:
		case TYPE_4:
			retVal = (float) (Math.sqrt(tmp) + circleDot.y);
			break;
		case TYPE_5:
		case TYPE_6:
		case TYPE_7:
		case TYPE_8:
			retVal = (float) (-Math.sqrt(tmp) + circleDot.y);
			break;
		}
		return retVal;
	}
	@Override
	public Vector2 getBegin() {
		return begin;
	}

	@Override
	public Vector2 getEnd() {
		return end;
	}

	public Vector2 getCircleDot() {
		return circleDot;
	}

	public void setCircleDot(Vector2 circleDot) {
		this.circleDot = circleDot;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
